var app = getApp(),
  that;
Page({
  data: {
    selectPerson: false,
    selectArea: false,
    selectCar: true,
    carArea: false,
    moreArea: false,
    selectMore: true,
    hid: true,
    memberShip: [],
    deadline1: '',
    price1: '',
    isshow: {},
    person_list: [{
        'name': '客户基本信息',
        'click': 'clickPerson',
        'img': 'mumber.png',
        'index': '0'
      },
      {
        'name': '车辆基本信息',
        'click': 'clickCar',
        'img': 'car.png',
        'index': '1'
      },
      {
        'name': '其他信息',
        'click': 'clickMore',
        'img': 'more.png',
        'index': '2'
      }
    ],
    index: '',
    sex: '',
    nickName: "",
    avatarUrl: "",
    userName: '',
    mobile: '',
    first: '',
    Gender: 'female',
    casIndex: 0,
    storesIndex: 0,
    items: [{
        name: '0',
        value: '男',
        checked: 'true'
      },
      {
        name: '1',
        value: '女'
      },
    ],
    first: [{
        name: '0',
        value: '是',
        checked: 'true'
      },
      {
        name: '1',
        value: '否'
      },
    ],
    purpose: [{
        name: '0',
        value: '公'
      },
      {
        name: '1',
        value: '私',
        checked: 'true'
      },
    ],
    allGoodsFilte: [{
        name: '美食',
        value: '0',
        checked: true
      },
      {
        name: '影视',
        value: '1',
        checked: false
      },
      {
        name: '棋牌',
        value: '2',
        checked: false
      },
      {
        name: '探险',
        value: '3',
        checked: false
      },
      {
        name: '音乐',
        value: '4',
        checked: false
      },
      {
        name: '体育',
        value: '5',
        checked: false
      },
      {
        name: '旅游',
        value: '6',
        checked: false
      },
      {
        name: '泡吧',
        value: '7',
        checked: false
      },
      {
        name: '文学',
        value: '8',
        checked: false
      },
      {
        name: '摄影',
        value: '9',
        checked: false
      },
      {
        name: '购物',
        value: '10',
        checked: false
      },
      {
        name: '舞蹈',
        value: '11',
        checked: false
      },
      {
        name: '阅读',
        value: '12',
        checked: false
      },
      {
        name: '武术',
        value: '13',
        checked: false
      },
      {
        name: '投资',
        value: '14',
        checked: false
      },
      {
        name: '垂钓',
        value: '15',
        checked: false
      },
      {
        name: '健身',
        value: '16',
        checked: false
      },
      {
        name: '游泳',
        value: '17',
        checked: false
      },
      {
        name: '运动',
        value: '18',
        checked: false
      },
      {
        name: '打球',
        value: '19',
        checked: false
      },
    ],

  },
  radiosSexChange: function(e) {


    if (e.detail.value == '0') {
      this.setData({
        sex: 0
      })
    } else {
      this.setData({
        sex: 1
      })
    }
  },
  radiosFirstChange: function(e) {
    if (e.detail.value == '0') {
      this.setData({
        first: 0
      })
    } else {
      this.setData({
        first: 1
      })
    }
  },
  radioEffectChange: function(e) {
    if (e.detail.value == '0') {
      this.setData({
        Gender: 0
      })
    } else {
      this.setData({
        Gender: 1
      })
    }
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    that = this
    console.log(options)
    var n = this.data.person_list
    n[0].open = true
    this.setData({
      person_list: n
    });
  },
  onReady: function(e) {
    // 页面渲染完成
  },
  onShow: function() {
    //会员卡权限
    app.getData({
      url: app.api.getMemberShip(),
      method: 'GET',
      data: '',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            console.log(res.data.data.isopen)
            if (res.data.data.isopen == 0) {
              that.setData({
                memberShip: res.data.data.deadlineSets,
              })
            }
            that.setData({
              // memberShip: res.data.data.deadlineSets,
              isshow: res.data.data.isopen
            })
            console.log(res.data.data.isopen)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      }
    })
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭

  },
  radioChange: function(t) {
    console.log(t)
    this.setData({
      radioChange: t.detail.value
    }), console.log("radio发生change事件，携带value值为：", JSON.parse(t.detail.value));
  },
  //点击选择类型
  kindToggle: function(o) {
    var t = o.currentTarget.id,
      n = this.data.person_list;
    console.log(t);
    for (var a = 0, e = n.length; a < e; ++a) n[a].open = a == t && !n[a].open;
    console.log(n)
    this.setData({
      person_list: n
    });
  },
  carInfo: function(e, res) {
    console.log(e)
    console.log(res)
    app.getData({
      url: app.api.getCarData(),
      method: 'POST',
      data: {
        memberBaseId: res,
        carNumber: e.carid,
        vin: e.vin,
        purchaseDate: e.buydate,
        model: e.carmodel
      },
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            wx.navigateBack({});
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      }

    })
  },
  //客户基本信息
  bindSave: function(e) {
    var that = this;
    // that.carInfo(e.detail.value, res.data.data.id);
    console.log(e)
    console.log(that.data.isshow)
    var formData = e.detail.value;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/; //手机号
    var carCard = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/; //车牌号
    var cjh = "^(\\d{17})$"
    // console.log(formData)
    if (formData.userName == '') {
      wx.showToast({
        title: '用户名不能为空！',
        icon: 'none'
      })
    } else if (!(/^[\u4e00-\u9fa5]{2,4}$/.test(formData.userName))) {
      wx.showToast({
        title: '用户名不正确！',
        icon: 'none'
      })
    } else if (formData.mobile == '') {
      wx.showToast({
        title: '手机号不能为空！',
        icon: 'none'
      })
    } else if (!myreg.test(formData.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号!',
        icon: 'none'
      })
    } else if (formData.carid == '') {
      wx.showToast({
        title: '车牌号不能为空！',
      })
    } else if (!(carCard.test(formData.carid))) {
      wx.showToast({
        title: '车牌号有误！',
      })
    } else if (formData.vin == '') {
      wx.showToast({
        title: '车架号不能为空！',
      })
    } else {
      // console.log(that.data.memberShip)
      if (that.data.isshow == 1) {
        // loginSubmitted: function() {
        app.getData({
          url: app.api.regUserInfo(),
          method: 'POST',
          data: {
            openid: wx.getStorageSync('openId'),
            memberName: formData.userName,
            idNumber: formData.idcard,
            sex: formData.sex,
            address: formData.address,
            phone: formData.mobile,
            isFirstcar: formData.isFirst,
            carUser: formData.effect,
            bakName: formData.otheruserName,
            bakPhone: formData.othermobile,
            memberLevelId: '100002',
            companyId: app.globalData.companyId,
          },
          ok: function(res) {
            console.log(res)
            if (res.statusCode == 200 && res.errMsg == 'request:ok') {
              if (res.data.code == 1) {
                '用户信息保存成功！' == res.data.message &&
                  (wx.showLoading({
                      title: '注册中...',
                      mask: !0
                    }),
                    app.globalData.memberLevelId = res.data.data.memberLevelId,
                    wx.setStorageSync('bizUserInfo', res.data.data),
                    setTimeout(function() {
                      that.carInfo(e.detail.value, res.data.data.id)
                    }, 1000))
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none'
                })
              }
            }
          }
        })
        // }
      } else {
        if (e.detail.value.radiogroup == '') {
          wx.showToast({
            title: '请先选择会员卡时间！',
            icon: 'none'
          })
        } else {
          app.getData({
            url: app.api.getLogin(),
            method: "POST",
            data: {
              memberId: wx.getStorageSync('bizUserInfo').id,
              companyId: app.globalData.companyId,
              amt: that.data.memberShip[that.data.radioChange].price,
              // amt: 0.01,
              cardExpire: that.data.memberShip[that.data.radioChange].deadline,
              cardId: 100002,
            },
            ok: function(t) {
              console.log(t)
              //  添加订单成功之后去支付
              var openId = wx.getStorageSync("openId")
              // var openId = "wxd2188c60cd7f825f";
              app.getData({
                url: app.api.toPay(t.data.data.id, openId, 5), //仅为示例，并非真实的接口地址
                method: 'GET',
                ok: function(k) {
                  console.log(k)
                  wx.requestPayment({
                    timeStamp: JSON.stringify((JSON.parse(k.data.params)).timeStamp),
                    nonceStr: (JSON.parse(k.data.params)).nonceStr,
                    package: (JSON.parse(k.data.params)).package,
                    signType: 'MD5',
                    paySign: (JSON.parse(k.data.params)).paySign,
                    success: function() {
                      app.getData({
                        url: app.api.regUserInfo(),
                        method: 'POST',
                        data: {
                          openid: wx.getStorageSync('openId'),
                          memberName: formData.userName,
                          idNumber: formData.idcard,
                          sex: formData.sex,
                          address: formData.address,
                          phone: formData.mobile,
                          isFirstcar: formData.isFirst,
                          carUser: formData.effect,
                          bakName: formData.otheruserName,
                          bakPhone: formData.othermobile,
                          memberLevelId: '100002',
                            companyId: app.globalData.companyId,
                        },
                        ok: function(res) {
                          console.log(res)
                          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
                            if (res.data.code == 1) {
                              '用户信息保存成功！' == res.data.message &&
                                (wx.showLoading({
                                    title: '注册中...',
                                    mask: !0
                                  }),
                                  app.globalData.memberLevelId = res.data.data.memberLevelId,
                                  wx.setStorageSync('bizUserInfo', res.data.data),
                                  setTimeout(function() {
                                    that.carInfo(e.detail.value, res.data.data.id)
                                  }, 1000))
                            } else {
                              wx.showToast({
                                title: res.data.message,
                                icon: 'none'
                              })
                            }
                          }
                        }
                      })
                    },
                    fail: function(t) {
                      console.log(t)
                      wx.showToast({
                        title: '支付失败',
                        duration: 3000
                      })
                    },
                    complete: function(t) {}
                  })
                }
              })
            }
          })
        }
      }
    }
  },
  wxParseTagATap: function (e) {
    console.log(e)
    // var href = e.currentTarget.dataset.src;
    // console.log(href);
    // //我们可以在这里进行一些路由处理
    // if (href.indexOf(index) > 0) {
    //   // wx.redirectTo({
    //   //   url: '../index/index'
    //   // })

    // }

  }
})